<template>
  <div class="chart" :style="chartStyle">
    <div id="pieEchart" :style="pieStyle" />
    <div id="lineEchart" :style="lineStyle" />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
// import { Chart } from '@vue-chart/echart';
import * as echarts from 'echarts';
import { RateList } from '../index/commentAnalysis';
import { tableDataType } from './sku';

const emit = defineEmits([
 'update:isLoading'
])

const props = defineProps<{
  commentSourceData: RateList[],
  skuList: tableDataType[],
  isLoading: boolean
}>();

const chartStyle = ref({})
const pieStyle = ref({})
const lineStyle = ref({})

const pieEchart = ref<echarts.ECharts | null>(null)
const lineEchart = ref<echarts.ECharts | null>(null)
const pieOption = ref({
  title: {
    left: 'center'
  },
  tooltip: {
    trigger: 'item'
  },
  legend: {
    orient: 'vertical',
    type: 'scroll',
    left: 'left',
    top: 20,
    bottom: 20,
  },
  series: [
    {
      name: '名称',
      type: 'pie',
      radius: '50%',
      data: [] as {name: string, value: number}[],
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.8)'
        }
      }
    }
  ]
})
const lineOption = ref({
  grid: {
    left: '5%',
    right: '5%',
    bottom: '100px',
    containLabel: true
  },
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'cross'
    },
  },
  dataZoom: [
    {
      type: 'inside',
      xAxisIndex: [0],
      start: 0,
      end: 100
    },
    {
      show: true,
      xAxisIndex: [0],
      type: 'slider',
      bottom: 40,
      start: 0,
      end: 100
    }
  ],
  legend: {
    type: 'scroll',
    data: [] as string[]
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: [] as string[]
  },
  yAxis: {
    type: 'value'
  },
  series: [] as { name: string; type: string; stack: string; data: number[] }[]
})
const setlineChartData = () => {
  const xAxisData: string[] = [];
  const legendData: string[] = [];
  const seriesData: { name: string; type: string; stack: string; data: number[] }[] = [];
  props.commentSourceData.sort((a,b) => new Date(a.feedbackDate).getTime() - new Date(b.feedbackDate).getTime())
  props.commentSourceData.forEach(comment => {
    if (!xAxisData.includes(comment.feedbackDate)) {
      xAxisData.push(comment.feedbackDate);
    }
  });
  props.skuList.forEach(item => {
    const propPathValues = item.propPath.split(';').filter(Boolean).map(pair => pair.split(':')[1]);
    const dataItem = {
      name: item.name,
      type: 'line',
      stack: '',
      data: xAxisData.map(date => {
         const count = props.commentSourceData
          .filter(comment => comment.feedbackDate === date && propPathValues.some(val => comment.skuVids ? comment.skuVids.split('#').includes(val) : ''))
          .length;
        return count;
      }),
    };
    seriesData.push(dataItem);
    legendData.push(item.name);
  });
  console.log('🚀 ~ setlineChartData ~ seriesData:',legendData);
  console.log('🚀 ~ setlineChartData ~ seriesData:',xAxisData);
  console.log('🚀 ~ setlineChartData ~ seriesData:',seriesData);
  
  lineOption.value.legend.data = legendData;
  lineOption.value.xAxis.data = xAxisData;
  lineOption.value.series = seriesData;
  nextTick(() => {
    if (lineEchart.value) {
      lineEchart.value.setOption(lineOption.value, true);
      lineEchart.value.resize(); // 强制重绘图表
    }
  })
}
const setPieChartData = () => {
  pieOption.value.series[0].data = []
  props.skuList.forEach(item => {
    pieOption.value.series[0].data.push({
      name: item.name,
      value: item.commentNumber,
    })
  })
  nextTick(() => {
    if (pieEchart.value) {
      pieEchart.value.setOption(pieOption.value, true);
      pieEchart.value.resize(); // 强制重绘图表
    }
  })
}
const setStyle = () => {
  if (props.skuList.length > 100) {
    chartStyle.value = {
      display: 'flex',
      'flex-direction': 'column',
      overflow: 'hidden auto',
      height: '800px',
    }
    pieStyle.value = {
      height: '400px',
      width: '100%',
    }
    lineStyle.value = {
      height: '400px',
      width: '100%',
      'margin-top': '20px'
    }
  }else {
    chartStyle.value = {
      height: '400px',
      display: 'flex',
      'flex-direction': 'row',
    }
    pieStyle.value = {
      height: '400px',
      width: '40%',
    }
    lineStyle.value = {
      'margin-left': '30px',
      height: '400px',
      width: '60%',
    }
  }
}
const setChartData = () => {
  setStyle()
  setPieChartData()
  setlineChartData()
  emit('update:isLoading', false)
}
onMounted(() => {
  pieEchart.value =  markRaw(echarts.init( document.getElementById('pieEchart')))
  lineEchart.value =  markRaw(echarts.init( document.getElementById('lineEchart')))
})
watch(() => props, (newVal, oldVal) => {
  setChartData()
},{deep:true})
</script>

<style lang="less" scoped>
.chart {
  width: 100%;
}
</style>